﻿/*
 * @author 冷斌
 * 2012-3-25 添加 GetAllExamInfoByRoomNum
 */
package cn.edu.zjusc.Webservice;

import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.interceptor.Interceptors;
import javax.jws.*;
import javax.jws.soap.SOAPBinding;
import javax.persistence.*;

import org.jboss.wsf.spi.annotation.WebContext;

import cn.edu.zjusc.POJO.*;
import cn.edu.zjusc.Security.ProfileInterceptor;
import cn.edu.zjusc.Security.UserCave;
import cn.edu.zjusc.Util.PersistenceHelper;
@Stateless
@WebService( name = "ExamService", serviceName = "ExamService")   
@SOAPBinding(style = SOAPBinding.Style.RPC)
@WebContext (contextRoot="/ZJUSCIX/Service", urlPattern="/ExamService") 
public class ExamService{
	
	/**
	 * 获取所有考试
	 * @param strName
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<ExamInfo> GetAllExamInfo() {

		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		Date date=new Date();
		
		Query query=em.createQuery("from ExamInfo e");
		List examList=query.getResultList();
		
		em.close();
		emf.close();
		return examList;
	}

	/**
	 * 新增一条考试
	 * @param eiBean
	 * @return
	 */
	@Interceptors(ProfileInterceptor.class)
	public String NewExamInfo(@WebParam(name = "uc") UserCave uc,
			@WebParam(name = "examEntry") ExamInfo eiBean) {
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		//开始事务
		em.getTransaction().begin();
		em.persist(eiBean);
		//提交事务
		em.getTransaction().commit();
		
		em.close();
		emf.close();
		
		return "新增考试条目成功";
	}
	
	/**
	 * 修改一条考试
	 * @param eiBean
	 * @return
	 */
	@Interceptors(ProfileInterceptor.class)
	public String ModifyExamInfo(@WebParam(name = "uc") UserCave uc,
			@WebParam(name = "examEntry") ExamInfo eiBean) {
		EntityManagerFactory emf =Persistence.createEntityManagerFactory("zjusc");
		EntityManager em=emf.createEntityManager();
		
		//开始事务
		em.getTransaction().begin();

		em.merge(eiBean);
		//提交事务
		em.getTransaction().commit();
		
		em.close();
		emf.close();
		
		return "修改考试条目成功";
	}
	
	/**
	 * 删除一条考试
	 * @param examID
	 * @return
	 */
	@Interceptors(ProfileInterceptor.class)
	public String DeleteExamInfo(@WebParam(name = "uc") UserCave uc,@WebParam(name = "id") int examID) {
		
		PersistenceHelper.deleteEntity(ExamInfo.class, examID);
		return "删除考试条目成功";
	}

	
	/**
	 * 获得一个考场里的所有考试
	 * @param RoomNum
	 */
	@SuppressWarnings("unchecked")
	public List<ExamInfo> GetAllExamInfoByRoomNum(@WebParam(name="RoomNum")int roomNum ) {

		
		String hql = "from ExamInfo e where e.roomId=?1";
		Object[] params = { roomNum };
		return PersistenceHelper.executeReader(hql, params);
	}
	
	
}
